/**
 * 载入项目引导文件
 */
require('./bootstrap');

window.Vue = require('vue');

const app = new Vue({
    el: '#wrapper',
    data: {
        box: null,
        asxBox:null,
        loadingBox:null,
        nowNode:null,
        roleSaveUrl:null,
        curRoleData:{
            role_id:0,
            parent_id:0,
            name:'',
            display_name:'',
            description:'',
            order_num:'',
            appid:'',
        },
        data_mplist:page_params.data_mplist,
    },
    methods: {
        initTree : function () {
            utils.ajaxGet(page_params.url_list, {}, (response, data) => {
                if (response.data.treeList) {
                    tree_data = response.data.treeList;
                    $('#tree').treeview({data: tree_data, levels: 99, showTags: !0, collapseIcon: 'fa fa-chevron-down', expandIcon: 'fa fa-chevron-right'});
                    //绑定选中事件
                    $('#tree').on('nodeSelected', (event, data) => {
                        this.asxBox = layer.open({
                            type: 1,
                            title: '角色操作',
                            anim: 2,
                            shadeClose: true, //开启遮罩关闭
                            skin: 'layui-layer-molv', //加上边框
                            area: ['520px', '150px'], //宽高
                            content: $('#box_ask')
                        });
                        this.nowNode = data;
                    });
                }
            });
        },
        roleAddTop:function () {//添加顶级角色
            this.roleSaveUrl = page_params.url_add;
            this.curRoleData = {
                role_id:0,
                parent_id:0,
                name:'',
                display_name:'',
                description:'',
                order_num:999,
            };
            this.box = layer.open({
                type: 1,
                title: '添加【顶级角色】',
                anim: 2,
                shadeClose: true, //开启遮罩关闭
                skin: 'layui-layer-molv', //加上边框
                area: ['620px', '480px'], //宽高
                content: $('#box-form')
            });
        },
        roleAddChild:function () {//添加子角色
            this.roleSaveUrl = page_params.url_add;
            this.curRoleData = {
                role_id:0,
                parent_id:this.nowNode.role_id,
                name:'',
                display_name:'',
                description:'',
                order_num:999,
            };
            this.box = layer.open({
                type: 1,
                title: '添加【' + this.nowNode.display_name + '】子角色',
                anim: 2,
                shadeClose: true, //开启遮罩关闭
                skin: 'layui-layer-molv', //加上边框
                area: ['620px', '480px'], //宽高
                content: $('#box-form')
            });
        },
        roleEdit:function () {//角色编辑
            this.roleSaveUrl = page_params.url_edit;
            this.curRoleData = _.cloneDeep(this.nowNode);
            this.box = layer.open({
                type: 1,
                title: '编辑角色',
                anim: 2,
                shadeClose: true, //开启遮罩关闭
                skin: 'layui-layer-molv', //加上边框
                area: ['620px', '480px'], //宽高
                content: $('#box-form')
            });
        },
        roleSave:function () {//角色保存
            utils.ajaxPost(this.roleSaveUrl, this.curRoleData, (response, postdata) => {
                data = response.data;
                utils.showToastr(data);
                if (data.status == 'success') {
                    layer.close(this.box);
                    layer.close(this.asxBox);
                    this.initTree();
                }
            });
        },
        roleDel:function () {//角色删除
            let role_id = 0;
            if (this.nowNode) {
                role_id = this.nowNode.role_id;
            }
            utils.confirm(page_params.url_del, {'id': role_id}, (response, postdata) => {
                data = response.data;
                utils.showToastr(data);
                if (data.status == 'success') {
                    layer.close(this.asxBox);
                    this.initTree();
                }
            });
        },
        permAppidChange:function(){
            this.permInit(this.curRoleData.appid);
        },
        permInit : function(appid = ''){
            if (_.isEmpty(appid)){
                this.curRoleData.appid = page_params.data_appid;
            }else{
                this.curRoleData.appid = appid;
            }
            this.loadingBox = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            utils.ajaxGet(page_params.url_get, {'role_id':this.nowNode.role_id, 'appid' : this.curRoleData.appid}, (response, postdata) => {
                data = response.data;
                if (data.status == 'success') {
                    this.curRoleData.permIds = data.data.roleData.permIds;
                }else{
                    this.curRoleData.permIds = [];
                }
                //取消选中jstree的所有节点
                $("#jstree").jstree('uncheck_all');
                // 批量选中节点
                $("#jstree").jstree('check_node', this.curRoleData.permIds);
                layer.close(this.loadingBox);
            });
        },
        permEdit : function(){//权限编辑
            // 加载树结构
            $('#jstree').jstree({
                'core': {
                    'data': page_params.data_perm
                },
                'types': {
                    'default': {
                        'icon': 'fa fa-folder'
                    },
                },
                "checkbox": {
                    "keep_selected_style": false
                },
                "plugins": ['types', 'checkbox'],
            });
            this.box = layer.open({
                type: 1,
                title: '编辑权限',
                anim: 2,
                shadeClose: true, //开启遮罩关闭
                skin: 'layui-layer-molv', //加上边框
                area: ['620px', '480px'], //宽高
                content: $('#box-form-perm')
            });
            //初始化权限数据
            this.permInit();
        },
        permSave:function () {//权限保存
            // 已选权限
            let checkedNode = $("#jstree").jstree('get_checked');
            utils.ajaxPost(page_params.url_perm_save, {role_id:this.nowNode.role_id,appid:this.curRoleData.appid,permIds: checkedNode}, (response, postdata) => {
                data = response.data;
                utils.showToastr(data);
                if (data.status == 'success') {
                    this.initTree();
                }
            });
        },
        permSaveExit:function () {
            this.permSave();
            layer.close(this.box);
            layer.close(this.asxBox);
        }
    },
    created:function () {
        this.initTree();
    }
});